package com.pzhu.dormitory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pzhu.dormitory.dao.CostDao;
import com.pzhu.dormitory.entity.CostEntity;
import com.pzhu.dormitory.entity.CostVo;
import com.pzhu.dormitory.entity.StudentEntity;
import com.pzhu.dormitory.service.CostService;
import com.pzhu.dormitory.service.StudentService;
import com.pzhu.dormitory.service.UserService;
import com.pzhu.dormitory.utils.PageUtils;
import com.pzhu.dormitory.utils.Query;
import com.pzhu.dormitory.vo.AbsentFromWorkVo;
import com.pzhu.dormitory.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Optional;


@Service("costService")
public class CostServiceImpl extends ServiceImpl<CostDao, CostEntity> implements CostService {

    @Autowired
    private  CostDao costDao;
    @Autowired
    private UserService userService;
    @Autowired
    private StudentService studentService;

    @Override
    public PageUtils<CostEntity> queryPage(Map<String, Object> params) {
        IPage<CostEntity> page = this.page(
                new Query<CostEntity>().getPage(params),
                new QueryWrapper<>()
        );
        return new PageUtils<>(page);
    }

    @Override
    public PageUtils<CostVo> queryPageCostVo(Map<String, Object> params) {
        Page<CostVo> page = new Query<CostVo>().getPage(params);
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserVo userVo = userService.getUserInfoByUsername(authentication.getName());
        Optional<StudentEntity> one = studentService.lambdaQuery().eq(StudentEntity::getSno, userVo.getUsername()).oneOpt();
        one.ifPresent(studentEntity -> {
            params.put("studentId",one.get().getId());
        });
        // 查询基本数据
        Page<CostVo> roomVoPage = costDao.queryPageVo(page, params);
        return new PageUtils<>(roomVoPage);
    }

}
